package com.wzt.lianfirecontrol.adapter.yighuan;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.function.yinhuan.YingHuanModel;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.Utils;

public class YingHuanListAdapter extends BaseRecyclerAdapter<YingHuanModel> {
    private Context context;

    private boolean needSetTopMargin = false;

    public boolean isNeedSetTopMargin() {
        return needSetTopMargin;
    }

    public void setNeedSetTopMargin(boolean needSetTopMargin) {
        this.needSetTopMargin = needSetTopMargin;
    }

    public YingHuanListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_info_list, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, YingHuanModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_item_content;
        private ImageView iv_title;
        private TextView tv_title;
        private TextView tv_flag;
        private TextView tv_status;
        private ImageView iv_show_goto;
        private LinearLayout ll_location;
        private ImageView iv_location;
        private TextView tv_location;
        private LinearLayout ll_person;
        private ImageView iv_person;
        private TextView tv_person;
        private TextView tv_person_phone;
        private LinearLayout ll_time;
        private ImageView iv_time;
        private TextView tv_time;


        public MyViewHolder(View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.iv_title = itemView.findViewById(R.id.iv_title);
        myViewHolder.tv_title = itemView.findViewById(R.id.tv_title);
        myViewHolder.tv_flag = itemView.findViewById(R.id.tv_flag);
        myViewHolder.tv_status = itemView.findViewById(R.id.tv_status);
        myViewHolder.iv_show_goto = itemView.findViewById(R.id.iv_show_goto);
        myViewHolder.ll_location = itemView.findViewById(R.id.ll_location);
        myViewHolder.iv_location = itemView.findViewById(R.id.iv_location);
        myViewHolder.tv_location = itemView.findViewById(R.id.tv_location);
        myViewHolder.ll_person = itemView.findViewById(R.id.ll_person);
        myViewHolder.iv_person = itemView.findViewById(R.id.iv_person);
        myViewHolder.tv_person = itemView.findViewById(R.id.tv_person);
        myViewHolder.tv_person_phone = itemView.findViewById(R.id.tv_person_phone);
        myViewHolder.ll_time = itemView.findViewById(R.id.ll_time);
        myViewHolder.iv_time = itemView.findViewById(R.id.iv_time);
        myViewHolder.tv_time = itemView.findViewById(R.id.tv_time);
    }

    private void setItemData(MyViewHolder viewHolder, YingHuanModel data, int position) {
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (0 == position && needSetTopMargin){
            layoutParams.setMargins(Utils.dip2px(context, 0), Utils.dip2px(context, 15),
                    Utils.dip2px(context, 0), Utils.dip2px(context, 15));
        }else {
            layoutParams.setMargins(Utils.dip2px(context, 0), Utils.dip2px(context, 0),
                    Utils.dip2px(context, 0), Utils.dip2px(context, 15f));
        }

        if (StringUtils.isEmpty(data.getName())){
            viewHolder.tv_title.setVisibility(View.GONE);
        }else {
            viewHolder.tv_title.setVisibility(View.VISIBLE);
            viewHolder.tv_title.setText(data.getName());
        }

        if (StringUtils.isEmpty(data.getStatus())){
            viewHolder.tv_status.setVisibility(View.GONE);
        }else {
            viewHolder.tv_status.setVisibility(View.VISIBLE);
            GradientDrawable gradientDrawable = new GradientDrawable();
            if ("0".equals(data.getStatus())){
                viewHolder.tv_status.setText("待整改");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.change_status_color));
            }else if ("2".equals(data.getStatus())){
                viewHolder.tv_status.setText("已整改");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.look_status_color));
            }else {
                viewHolder.tv_status.setText("整改中");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.main_color));
            }
            gradientDrawable.setCornerRadius(Utils.dip2px(context, 10));
            viewHolder.tv_status.setBackground(gradientDrawable);
        }
        StringBuilder stringBuilder = new StringBuilder();
        if (!StringUtils.isEmpty(data.getBuildingName())){
            stringBuilder.append(data.getBuildingName());
        }
        if (!StringUtils.isEmpty(data.getFloorName())){
            stringBuilder.append(data.getFloorName());
        }
        if (!StringUtils.isEmpty(data.getAddress())){
            stringBuilder.append(data.getAddress());
        }
        if (StringUtils.isEmpty(stringBuilder.toString())){
            viewHolder.ll_location.setVisibility(View.GONE);
        }else {
            viewHolder.ll_location.setVisibility(View.VISIBLE);
            viewHolder.iv_location.setVisibility(View.VISIBLE);
            viewHolder.tv_location.setText(stringBuilder.toString());
        }

        if (StringUtils.isEmpty(data.getRealName())) {
            viewHolder.ll_person.setVisibility(View.GONE);
        } else {
            viewHolder.iv_person.setVisibility(View.VISIBLE);
            viewHolder.ll_person.setVisibility(View.VISIBLE);
            viewHolder.tv_person.setText(data.getRealName());
            if (StringUtils.isEmpty(data.getPhone())) {
                viewHolder.tv_person_phone.setVisibility(View.GONE);
            } else {
                viewHolder.tv_person_phone.setVisibility(View.VISIBLE);
                viewHolder.tv_person_phone.setText(data.getPhone());
            }
        }

        if (StringUtils.isEmpty(data.getOccurTimeStr())){
            viewHolder.ll_time.setVisibility(View.GONE);
        }else {
            viewHolder.ll_time.setVisibility(View.VISIBLE);
            viewHolder.iv_time.setVisibility(View.VISIBLE);
            viewHolder.tv_time.setText(data.getOccurTimeStr());
        }
        viewHolder.iv_show_goto.setVisibility(View.VISIBLE);
    }
}
